
package com.epam.android.training.day10.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.provider.BaseColumns;
import android.text.TextUtils;

import java.util.HashMap;

/**
 * Provides access to a secret database of secret contacts.
 */
public class SecretContactsDAO {

    private static HashMap<String, String> projectionMap;
    static {
        projectionMap = new HashMap<String, String>();
        projectionMap.put(SecretContacts._ID, SecretContacts._ID);
        projectionMap.put(SecretContacts.NAME, SecretContacts.NAME);
        projectionMap.put(SecretContacts.MAIL, SecretContacts.MAIL);
    }

    private final DatabaseHelper dbHelper;

    public SecretContactsDAO(Context context) {
        super();
        dbHelper = new DatabaseHelper(context);
    }

    public Cursor read(Long id, String[] projection) {
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        queryBuilder.setTables(SecretContacts.TABLE_NAME);
        queryBuilder.setProjectionMap(projectionMap);
        queryBuilder.appendWhere(SecretContacts._ID + "=" + id);
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = queryBuilder.query(db, projection, null, null, null, null, null);
        return cursor;
    }

    /**
     * Convenience method.
     */
    public Cursor readAll() {
        return readAll(null, null, null, null);
    }

    public Cursor readAll(String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        queryBuilder.setTables(SecretContacts.TABLE_NAME);
        queryBuilder.setProjectionMap(projectionMap);
        if (TextUtils.isEmpty(sortOrder)) {
            sortOrder = SecretContacts.DEFAULT_SORT_ORDER;
        }
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
        return cursor;
    }

    public long save(String name, String mail) {
        ContentValues values = createContentValues(name, mail);
        return dbHelper.getWritableDatabase().insert(SecretContacts.TABLE_NAME, SecretContacts.NAME, values);
    }

    public int delete(Long id) {
        return dbHelper.getWritableDatabase().delete(SecretContacts.TABLE_NAME, SecretContacts._ID + "=" + id, null);
    }

    public int update(Long id, String name, String mail) {
        ContentValues updateValues = createContentValues(name, mail);
        return dbHelper.getWritableDatabase().update(SecretContacts.TABLE_NAME, updateValues,
                SecretContacts._ID + "=" + id, null);
    }

    private ContentValues createContentValues(String name, String mail) {
        ContentValues values = new ContentValues();
        values.put(SecretContacts.NAME, name);
        values.put(SecretContacts.MAIL, mail);
        return values;
    }

    /**
     * Definitions for Contacts table
     */
    public final class SecretContacts implements BaseColumns {

        public static final String TABLE_NAME = "contacts";

        /**
         * The default sort order for this table
         */
        public static final String DEFAULT_SORT_ORDER = "name ASC";

        /**
         * Name of the contact
         * <P>
         * Type: TEXT
         * </P>
         */
        public static final String NAME = "name";

        /**
         * Mail address
         * <P>
         * Type: TEXT
         * </P>
         */
        public static final String MAIL = "mail";
    }

    private static class DatabaseHelper extends SQLiteOpenHelper {

        private static final String DATABASE_NAME = "contacts.db";

        private static final int DATABASE_VERSION = 4;

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + SecretContacts.TABLE_NAME + " (" + SecretContacts._ID
                    + " INTEGER PRIMARY KEY," + SecretContacts.NAME + " TEXT," + SecretContacts.MAIL + " TEXT" + ");");

            ContentValues values = new ContentValues();
            values.put(SecretContacts.NAME, "Dark Imperor");
            values.put(SecretContacts.MAIL, "dark_imperor@dark_universe.com");
            db.insert(SecretContacts.TABLE_NAME, SecretContacts.NAME, values);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + SecretContacts.TABLE_NAME);
            onCreate(db);
        }
    }
}
